<?php
class ml_model_rdsHash extends ml_model_redis  
{    
    const PREFIX_UID2NICK = 'u2n_';
    const PREFIX_NICK2UID = 'n2u_';
    private $u2n;
    function __construct() {
        if(!$this->init_rds('meila_cache'))
            return false;
    }
    
    /**
     * 
     * @param unknown_type $uid        用户uid
     * @param unknown_type $nick        新的Nick     
     */
    public function updateNick($uid,$nick) {

        $key = self::PREFIX_UID2NICK.$uid;
        //    先找出老的nickname
        $nickOld = $this->get($key);
//        $nickOld = $this->oRedis->get($key);
        // 找出原先nick生产的key  如果有就删除
        $keyOld = ml_tool_resid::getNickKey($nickOld);
        if($this->oRedis->exists($keyOld)) {
            $rs = $this->oRedis->delete($keyOld);
        }
        
        //    设置最新的nick
        $rs = $this->set($key,$nick);
        if(!$rs) return $rs;
        
        $keyNick = ml_tool_resid::getNickKey($nick);
        
        $rs = $this->set($keyNick,$uid);
        if(!$rs) return $rs;        
        
        return $rs;
    }
    
    /**
     * 根据uids 批量获取用户昵称
     * @param array $uids
     */
    

    

    public function getNicksByUids($aUid)
    {
        /**
         * @todo 失败情况
         */        
        if(!is_array($aUid))
            return false;
        
        foreach ($aUid as $uid)
            $aKeys[] = self::PREFIX_UID2NICK.$uid;
        
        $aRs = $this->oRedis->mget($aKeys);
        
        $this->set_data( array_combine($aUid , $aRs));
        return true;
    }
    
    public function getUidsByNicks($aNicks)
    {
        /**
         * @todo 失败情况
         */
        
        if(!is_array($aNicks))
            return false;
        
        foreach ($aNicks as $nick)
            $aKeys[] = ml_tool_resid::getNickKey($nick);
        
        $aRs = $this->mget($aKeys);
        /* 
        try {
            $aRs = $this->oRedis->mget($aKeys);
        }catch(Exception $oRedis) {            
            Tool_logger::debugLog('getUidsByNicks' , 'keys |'. implode(',', $aKeys));
            $aRs = array();
        }
         */
        $this->set_data( array_combine($aNicks , $aRs));
        return true;
    }
    
    /**
     * 根据uid获得昵称
     * @param int $uid
     * @return unknown
     */
    public function getNickByUid($uid) {
        if(isset($this->u2n[$uid]))
            return $this->u2n[$uid];
        
        $key = self::PREFIX_UID2NICK.$uid;
        $rs = $this->get($key);
        //如果失败 redis里没有 从库里查找,并放到redis里 以备下次查找
        if(!$rs){
            $oAccount = new ml_model_dbUserAccount();
            $oAccount->getAccountById($uid);
            $accountData = $oAccount->get_data();            
            $rs = $accountData['nickname'];
            $this->set($key,$accountData['nickname']);
        }
        $this->u2n[$uid]=$rs;
        return $rs;
        
    }
    
    

    
}